package helperclasses;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

public class InitialParams {

	private String numeCompanie;
	private String restaurant1;
	private String gestionar;

	private Connection getConnection() throws SQLException {

		DataSource ds = DbUtil.getInstance().getDataSource();
		Connection conn = ds.getConnection();
		return conn;
	}

	public String getNumeCompanie() {
		return numeCompanie;
	}

	public void setNumeCompanie(String numeCompanie) {
		this.numeCompanie = numeCompanie;
	}

	public String getRestaurant1() {
		return restaurant1;
	}

	public void setRestaurant1(String restaurant1) {
		this.restaurant1 = restaurant1;
	}

	public String getGestionar() {
		return gestionar;
	}

	public void setGestionar(String gestionar) {
		this.gestionar = gestionar;
	}

	public InitialParams() {

	}

	public void getInitParamsFromFile(String filePath) {
		Properties prop = new Properties();

		try {
			prop.load(new FileReader(new File(filePath)));

			setNumeCompanie(prop.getProperty("numecompanie"));
			setRestaurant1(prop.getProperty("restaurant1"));
			setGestionar(prop.getProperty("gestionar"));

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void getInitParamsFromDB() throws SQLException {
		Connection conn = getConnection();
		Statement st = conn.createStatement();
		String sql1, sql2, sql3;
		ResultSet result;
		sql1 = "SELECT * FROM gestionar";
		sql2 = "SELECT * FROM company";
		sql3 = "SELECT * FROM restaurant";
		try {
			//gestionar
			result = st.executeQuery(sql1);
			while (result.next()) {
				setGestionar(result.getString("nume"));
			}
			result.close();
			//company
			result = st.executeQuery(sql2);
			while (result.next()) {
				setNumeCompanie(result.getString("nume"));
			}
			result.close();
			//restaurant
			result = st.executeQuery(sql3);
			while (result.next()) {
				setRestaurant1(result.getString("nume"));
			}
			result.close();

		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			st.close();
			conn.close();
		}
	}
}
